﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace QiAdmin.Core
{
    public class ApiJsonResult : ActionResult, IStatusCodeActionResult
    {
        public int? StatusCode { get; set; } = 200;
        public string? ContentType { get; set; }

        public ResultType Code { get; set; } = ResultType.Success;
        public string? Msg { get; set; } = "操作成功";
        public dynamic? Value { get; set; }

        public ApiJsonResult(ResultType code, string? msg, object? value)
        {
            Code = code;
            if (string.IsNullOrEmpty(msg))
            {
                Msg = code == ResultType.Success ? "操作成功" : "操作失败";
            }
            else
            {
                Msg = msg;
            }
            Value = value;
        }
        public ApiJsonResult(ResultType code, string? msg) : this(code, msg, null)
        {
        }
        public ApiJsonResult(object value) : this(ResultType.Success, null, null)
        {
            Value = value;
        }

        public static ApiJsonResult Error => new ApiJsonResult(ResultType.Error, "操作失败");
        public static ApiJsonResult Success => new ApiJsonResult(ResultType.Success, "操作成功");
        public static ApiJsonResult Warning => new ApiJsonResult(ResultType.Warning, "Warning");

        public static ApiJsonResult Convert(bool exp, string msg = null)
        {
            return new ApiJsonResult(exp ? ResultType.Success : ResultType.Error, msg);
        }

        public static ApiJsonResult Convert(CommonResult commonResult)
        {
            return new ApiJsonResult(commonResult.Code, commonResult.Msg);
        }
        public static ApiJsonResult Convert<T>(CommonResult<T> commonResult, bool isConvertValue = true)
        {
            if (isConvertValue)
            {
                return new ApiJsonResult(commonResult.Code, commonResult.Msg, commonResult.Result);
            }
            else
            {
                return new ApiJsonResult(commonResult.Code, commonResult.Msg);
            }
        }
        public override void ExecuteResult(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentException("序列化内容不能为null");
            }
            if (context.HttpContext == null || context.HttpContext.Response == null)
            {
                return;
            }
            if (!context.HttpContext.Response.HasStarted)
            {
                context.HttpContext.Response.StatusCode = (int)StatusCode;

                context.HttpContext.Response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType;
            }

            Dictionary<string, object> dic = new Dictionary<string, object> {
                { "Code", Code },
                { "Msg", Msg },
                { "Data", Value }
            };

            IsoDateTimeConverter dateFormat = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            context.HttpContext.Response.WriteAsync(JsonConvert.SerializeObject(dic, Formatting.None, dateFormat));

        }

    }
    public enum ResultType
    {
        Error = 0,
        Success = 1,
        Warning = 2,
        Fail = -1
    }
}
